package com.soa.demo;


import com.alibaba.fastjson.JSONObject;
import com.soa.demo.util.GetWeather;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.Map;


/**
 * @Description:
 * @Author: Jukomu
 * @Package: com.soa.demo
 * @Project: APIService
 * @name: SmartHomeService
 * @Date: 2024/12/12-11:10
 * @Filename: SmartHomeService
 */

@RestController
@Api(value = "API控制类", tags = {"API控制类"})
public class SmartHomeService {
    @ApiOperation(value = "加法运算", notes = "输入整数a,b，返回res=a+b", response = String.class)
    @ApiImplicitParams({@ApiImplicitParam(name = "a", value = "加法数a", required = true, dataType = "int"), @ApiImplicitParam(name = "b", value = "加法数b", required = true, dataType = "int")})
    @RequestMapping(value = {"/add"}, method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public String add(@RequestParam(name = "a") int a, @RequestParam(name = "b") int b) {
        JSONObject result = new JSONObject();
        try {
            result.put("res", a + b);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result.toString();
    }

    @ApiOperation(value = "减法运算", notes = "输入整数a,b，返回res=a-b", response = String.class)
    @ApiImplicitParams({@ApiImplicitParam(name = "a", value = "被减数a", required = true, dataType = "int"), @ApiImplicitParam(name = "b", value = "减数b", required = true, dataType = "int")})
    @RequestMapping(value = {"/sub"}, method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public String sub(@RequestParam(name = "a") int a, @RequestParam(name = "b") int b) {
        JSONObject result = new JSONObject();
        try {
            result.put("res", a - b);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result.toString();
    }

    @ApiOperation(value = "乘法运算", notes = "输入整数a,b，返回res=a*b", response = String.class)
    @ApiImplicitParams({@ApiImplicitParam(name = "a", value = "乘数a", required = true, dataType = "int"), @ApiImplicitParam(name = "b", value = "乘数b", required = true, dataType = "int")})
    @RequestMapping(value = {"/mul"}, method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public String mul(@RequestParam(name = "a") int a, @RequestParam(name = "b") int b) {
        JSONObject result = new JSONObject();
        try {
            result.put("res", a * b);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result.toString();
    }

    @ApiOperation(value = "实时查询天气", notes = "输入城市名city，返回最低温度temp1,最高温度temp2,天气描述weather", response = String.class)
    @ApiImplicitParams({@ApiImplicitParam(name = "city", value = "城市名", required = true, dataType = "String")})
    @RequestMapping(value = {"/weather"}, method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public String getWeather(@RequestParam(name = "city") String city) {
        try {
            Map<String, String> weather = GetWeather.getWeather(city);
            JSONObject result = new JSONObject();
            result.put("city", city);
            result.put("citycode", weather.get("citycode"));
            result.put("weather", weather.get("textDay"));
            result.put("temp1", weather.get("tempMin"));
            result.put("temp2", weather.get("tempMax"));
            result.put("status", "ok");
            return result.toString();
        } catch (Exception e) {
            e.printStackTrace();
            return "{'status':'error'}";
        }
    }

    @RequestMapping(value = {"/setTemp"}, method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    @ApiOperation(value = "设置空调温度", notes = "传入设备id和空调温度值，返回结果温度和状态", response = String.class)
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "设备id", required = true, dataType = "String"), @ApiImplicitParam(name = "temp", value = "设置空调温度", required = true, dataType = "int")})
    public String setTemp(@RequestParam(name = "id") String id, @RequestParam(name = "temp") int temp) {
        JSONObject result = new JSONObject();
        try {
            result.put("id", id);
            result.put("temp", temp);
            result.put("status", "ok");

        } catch (Exception e) {
            e.printStackTrace();
        }
        return result.toString();
    }

    @ApiOperation(value = "设置开关窗", notes = "输入设备id和开关窗状态open(true or false),返回状态status和结果open", response = String.class)
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "设备id", required = true, dataType = "String"), @ApiImplicitParam(name = "open", value = "设置开关窗", required = true, dataType = "boolean")})
    @RequestMapping(value = {"/setWindow"}, method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public String openWindow(@RequestParam(name = "id") String id, @RequestParam(name = "open") boolean open) {
        JSONObject result = new JSONObject();
        try {
            result.put("id", id);
            result.put("open", open);
            result.put("status", "ok");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result.toString();
    }
}
